using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.TestDevice.Manager.Interface.Services;
using System.Collections.Generic;
using Microsoft.Practices.CompositeUI.EventBroker;
using Jbe.TestSuite.TestDevice.Manager.Constants;

namespace Jbe.TestSuite.TestDevice.Manager.Views
{
    public partial class TestDevicesViewPresenter : Presenter<ITestDevicesView>
    {
        [EventPublication(EventTopicNames.SelectedControlViewsChanged, PublicationScope.WorkItem)]
        public event EventHandler<EventArgs<IList<IControlView>>> SelectedControlViewsChanged;

        /// <summary>
        /// This method is a placeholder that will be called by the view when it has been loaded.
        /// </summary>
        public override void OnViewReady()
        {
            base.OnViewReady();
        }

        /// <summary>
        /// Close the view
        /// </summary>
        public void OnCloseView()
        {
            base.CloseView();
        }

        [EventSubscription(EventTopicNames.ControlViewsChanged, ThreadOption.UserInterface)]
        public void ControlViewsChanged(object sender, EventArgs<IList<IControlView>> e) 
        {
            View.UpdateControlViews(e.Data);
        }

        public void UpdateSelectedControlViews(IList<IControlView> devices)
        {
            OnSelectedControlViewsChanged(devices); 
        }

        protected virtual void OnSelectedControlViewsChanged(IList<IControlView> devices)
        {
            if (SelectedControlViewsChanged != null) 
            { 
                SelectedControlViewsChanged(this, new EventArgs<IList<IControlView>>(devices)); 
            }
        }
    }
}
